<?php 
//In order to insert elements into the db you can:
// -> go to sql.mit.edu, login with user= johnnyb and pass = biz13taf use phpMyAdmin
// -> in terminal run mysql -u johnnyb -p --host sql.mit.edu


$db_hostname = 'sql.mit.edu';
$db_database = 'johnnyb+childfeed';
$db_username = 'johnnyb';
$db_password = 'biz13taf';

//connects to server
$db_server = mysql_connect($db_hostname, $db_username, $db_password);

if (!$db_server){
  die("Unable to connect to MySQL: " . mysql_error());
}
//else{
//  echo "Successfully Connected to the database!";
//}

//selects childfeed database
mysql_select_db($db_database) or die ('Unable to select database: ' . mysql_error());

if (!function_exists('json_encode')) {
	function json_encode($data) {
		switch ($type = gettype($data)) {
			case 'NULL':
				return 'null';
			case 'boolean':
				return ($data ? 'true' : 'false');
			case 'integer':
			case 'double':
			case 'float':
				return $data;
			case 'string':
				return '"' . addslashes($data) . '"';
			case 'object':
				$data = get_object_vars($data);
			case 'array':
				$output_index_count = 0;
				$output_indexed = array();
				$output_associative = array();
				foreach ($data as $key => $value) {
					$output_indexed[] = json_encode($value);
					$output_associative[] = json_encode($key) . ':' . json_encode($value);
					if ($output_index_count !== NULL && $output_index_count++ !== $key) {
						$output_index_count = NULL;
					}
				}
				if ($output_index_count !== NULL) {
					return '[' . implode(',', $output_indexed) . ']';
				} else {
					return '{' . implode(',', $output_associative) . '}';
				}
			default:
				return ''; // Not supported
		}
	}
}
?>
